package kosta.model.board;

import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class QnABoardDaoImpl implements BoardDao {
	@Resource(name = "sqlSessionTemplate")
	private SqlSessionTemplate sqlSessionTemplate;

	@Override
	// 게시판 글 목록 보기
	public List<BoardVO> getListBoard() throws SQLException {
		return this.sqlSessionTemplate.selectList("qnaboard.getListBoard");
	}

	@Override
	// 게시물 상세보기
	public BoardVO findByNo(int no) throws SQLException {
		return this.sqlSessionTemplate.selectOne("qnaboard.findByNo", no);
	}

	@Override
	// 글쓰기
	public int insertBoard(BoardVO bvo) throws SQLException {
		return sqlSessionTemplate.insert("qnaboard.insertBoard", bvo);
	}

	@Override
	// 글수정
	public void updateBoard(BoardVO bvo) throws SQLException {
		sqlSessionTemplate.update("qnaboard.updateBoard", bvo);
	}

	@Override
	// 글삭제
	public void deleteBoard(int no) throws SQLException {
		sqlSessionTemplate.delete("qnaboard.deleteBoard", no);

	}

	@Override
	// 조회수 증가
	public void updateCount(int no) throws SQLException {
		sqlSessionTemplate.update("qnaboard.updateCount", no);
	}

	@Override
	// 아이디로 검색
	public List<BoardVO> findById(String id) throws SQLException {
		return sqlSessionTemplate.selectList("qnaboard.findById", id);
	}

	@Override
	// 글제목으로 검색
	public List<BoardVO> findByTitle(String title) throws SQLException {
		return sqlSessionTemplate.selectList("qnaboard.findByTitle", title);
	}

	@Override
	public int totalContent() throws SQLException {
		return sqlSessionTemplate.selectOne("qnaboard.totalContent");
	}
	/*
	 * 댓글 채택
	 * (non-Javadoc)
	 * @see kosta.model.board.BoardDao#checkAdopt(int)
	 */
	@Override
	public void checkAdopt(int no) throws SQLException {	
		sqlSessionTemplate.update("qnaboard.checkAdopt", no);
	}
	/*
	 * 포인트 차감
	 * 질문 게시판에 글을 등록하면 50포인트씩 차감
	 * (non-Javadoc)
	 * @see kosta.model.board.BoardDao#pointMinus(java.lang.String)
	 */
	@Override
	public void pointMinus(String id) throws SQLException {
		sqlSessionTemplate.update("qnaboard.pointMinus",id);;
	}
	
	////관리자가 글이동하는 
	@Override
	public int moveBoard(BoardVO bvo) throws Exception {
		return sqlSessionTemplate.insert("qnaboard.moveBoard",bvo);
	}

	@Override
	public BoardVO findByRand() throws Exception {
		return this.sqlSessionTemplate.selectOne("qnaboard.findByRand");
	}
}
